package com.soft.mainsite.component.beetl.tag.cms;

import cn.hutool.core.convert.Convert;
import com.google.common.collect.Maps;
import com.soft.mainsite.common.utils.StrUtil;
import com.soft.mainsite.common.exception.CmsException;
import com.soft.mainsite.common.utils.CmsUtil;
import com.soft.mainsite.module.publishlog.service.PublishLogService;
import com.soft.mainsite.module.web.cms.service.CategoryService;
import com.soft.mainsite.module.web.cms.service.SiteService;
import com.soft.mainsite.mybatis.model.TCmsCategory;
import com.soft.mainsite.mybatis.model.TCmsSite;
import lombok.extern.slf4j.Slf4j;
import org.beetl.core.GeneralVarTagBinding;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * Description:内容列表标签
 *
 * @author Jin
 * @create 2017-05-26
 **/
@Slf4j
@Service
@Scope("prototype")
public class CategoryTag extends GeneralVarTagBinding {

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private SiteService siteService;

    @Autowired
    private PublishLogService publishLogService;

    @Value("${system.http.protocol}")
    private String httpProtocol;

    @Value("${system.http.host}")
    private String httpHost;

    @Value("${system.site.prefix}")
    private String sitePrefix;

    @Value("${system.site.subfix}")
    private String siteSubfix;

    @Override
    public void render() {
        HttpServletRequest request = (HttpServletRequest) ctx.getGlobal("request");
        Map result = Maps.newHashMap();
        Long categoryId = Convert.toLong(this.getAttributeValue("categoryId"));
        Integer isParent = Convert.toInt(this.getAttributeValue("isParent"));
        TCmsCategory category = categoryService.findById(categoryId);
        TCmsSite site = siteService.findById(category.getSiteId());
        if (CmsUtil.isNullOrEmpty(category)) {
            throw new CmsException("栏目[" + categoryId + "]不存在！");
        }
        if (isParent.equals(1) && category.getParentId() != 0) {
            category = categoryService.findById(category.getParentId());
        }

        try {
            result.put("categoryId", category.getCategoryId());
            result.put("categoryImg", category.getCategoryImg());
            result.put("categoryIcon", category.getCategoryIcon());
            result.put("alias", category.getAlias());
            result.put("categoryName", category.getCategoryName());
            result.put("categoryDescription", category.getCategoryDescription());
            result.put("content", category.getContent());
            result.put("url", !StrUtil.isBlank(category.getUrl()) ? category.getUrl() : request.getContextPath() + "/" + site.getSiteKey() + "/" + category.getAlias() + siteSubfix);
            result.put("more", !StrUtil.isBlank(category.getUrl()) ? category.getUrl() : request.getContextPath() + "/" + category.getAlias() + "/index_1" + siteSubfix);
            result.put("prototype", category);
            this.binds(result);
            this.doBodyRender();
        } catch (Exception e) {
            log.error("栏目标签渲染失败,栏目id：{}", categoryId, e);
            publishLogService.save(site.getSiteId(), categoryId, "", "栏目标签渲染失败,栏目id:" + categoryId);
            throw new CmsException("栏目标签渲染失败，" + "栏目id:" + categoryId);
        }
    }

}
